package com.app.bimo.commonbusiness.network;

import android.util.Log;

import com.app.bimo.commonbusiness.bean.BaseResult;
import com.app.bimo.commonbusiness.network.Exception.ApiException;
import com.app.bimo.commonbusiness.network.schedulers.SchedulerProvider;
import com.app.bimo.commonbusiness.util.CustomAutoDisposeConverter;
import com.uber.autodispose.AutoDisposeConverter;
import com.uber.autodispose.ObservableSubscribeProxy;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import okhttp3.ResponseBody;

public class RxObservableUtil {


    private RxObservableUtil() {
        throw new IllegalStateException("Can't instance the RxObservableUtil");
    }




    /**
     * 本地缓存订阅
     * @param observable
     * @param autoDisposeConverter
     * @param consumer
     * @param <T>
     */
//    public static <T> void subscribeLocal(Observable<T> observable, CustomAutoDisposeConverter<T> autoDisposeConverter, Consumer<T> consumer) {
//       observable.compose(SchedulerProvider.getInstance().applySchedulers())
//               .as(autoDisposeConverter.getAutoDisposeConverter())
//               .subscribe(new CommonSubscriber<T>(autoDisposeConverter.getiView(),autoDisposeConverter.isShowLoading(),autoDisposeConverter.isPageLoading(),null) {
//                   @Override
//                   public void onNext(T t) {
//                       super.onNext(t);
//                       try {
//                           consumer.accept(t);
//                       } catch (Exception e) {
//                           e.printStackTrace();
//                       }
//                   }
//               });
//    }

    /**
     * 网络订阅
     * @param observable
     * @param autoDisposeConverter
     * @param consumer
     * @param <T>
     */
    public static <T> void subscribe(Observable<BaseResult<T>> observable, CustomAutoDisposeConverter<T> autoDisposeConverter, Consumer<T> consumer) {
       subscribe(null,observable,autoDisposeConverter,consumer,null);
    }

    /**
     * 网络订阅，错误自定义处理
     * @param observable             被观察者
     * @param autoDisposeConverter   绑定生命周期
     * @param consumer               观察者
     * @param apiExceptionConsumer   自定义错误处理
     * @param <T>
     */
    public static <T> void subscribe(String url, Observable<BaseResult<T>> observable, CustomAutoDisposeConverter<T> autoDisposeConverter, Consumer<T> consumer,Consumer<ApiException> apiExceptionConsumer) {
        compose(observable)
                .as(autoDisposeConverter.getAutoDisposeConverter())
                .subscribe(new CommonSubscriber<T>(url,autoDisposeConverter.getiView(),autoDisposeConverter.isShowLoading(),autoDisposeConverter.isPageLoading(),apiExceptionConsumer) {
                    @Override
                    public void onNext(T t) {
                        super.onNext(t);
                        try {
                            consumer.accept(t);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    /**
     * test
     */
    public static <T> void subscribeTestCache(String url, Observable<BaseResult<T>> observable, CustomAutoDisposeConverter<T> autoDisposeConverter, Consumer<T> consumer,Consumer<ApiException> apiExceptionConsumer) {
        Log.i("testCache","test"+url);
        compose(observable)
                .as(autoDisposeConverter.getAutoDisposeConverter())
                .subscribe(new CommonSubscriber<T>(url,autoDisposeConverter.getiView(),autoDisposeConverter.isShowLoading(),autoDisposeConverter.isPageLoading(),apiExceptionConsumer) {
                    @Override
                    public void onNext(T t) {
                        super.onNext(t);
                        try {
                            consumer.accept(t);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }



    /**
     * 网络订阅，错误自定义处理  不绑定 之后改
     * @param observable
     * @param autoDisposeConverter
     * @param consumer
     * @param apiExceptionConsumer
     * @param <T>
     */
    public static <T> void subscribe(Observable<BaseResult<T>> observable, CustomAutoDisposeConverter<T> autoDisposeConverter, Consumer<T> consumer,Consumer<ApiException> apiExceptionConsumer) {
        compose(observable)
                .subscribe(new CommonSubscriber<T>("",autoDisposeConverter.getiView(),autoDisposeConverter.isShowLoading(),autoDisposeConverter.isPageLoading(),apiExceptionConsumer) {
                    @Override
                    public void onNext(T t) {
                        super.onNext(t);
                        try {
                            consumer.accept(t);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 下载文件订阅
     * @param observable
     * @param autoDisposeConverter
     * @return
     */
    public static ObservableSubscribeProxy subscribe(Observable<ResponseBody> observable, AutoDisposeConverter<ResponseBody> autoDisposeConverter) {
        return observable.compose(SchedulerProvider.getInstance().applySchedulers())
                .as(autoDisposeConverter);
    }

    /**
     * Rx操作符，把BaseResult转换成T
     * @param observable
     * @param <T>
     * @return
     */
    public static <T> Observable<T> compose(Observable<BaseResult<T>> observable) {
        return observable.compose(ResponseTransformer.handleResult())
                .compose(SchedulerProvider.getInstance().applySchedulers());
    }

    /**
     * Rx操作符，把BaseResult转换成T,网络访问错误时候不发送Error
     * @param observable
     * @param <T>
     * @return
     */
    public static <T> Observable<T> composeHaveCache(Observable<BaseResult<T>> observable) {
        return observable.compose(ResponseTransformer.handleResultHaveCache())
                .compose(SchedulerProvider.getInstance().applySchedulers());
    }

    /**
     * 网络订阅不需要转换操作符
     * @param observable
     * @param autoDisposeConverter
     * @param consumer
     * @param <T>
     */
//    public static <T> void subscribeWithOutCompose(Observable<T> observable, CustomAutoDisposeConverter<T> autoDisposeConverter, Consumer<T> consumer) {
//       subscribeWithOutCompose(observable,autoDisposeConverter,consumer,null);
//    }

    /**
     * 网络订阅不需要转换操作符，错误自定义
     * @param observable
     * @param autoDisposeConverter
     * @param consumer
     * @param apiExceptionConsumer
     * @param <T>
     */
//    public static <T> void subscribeWithOutCompose(Observable<T> observable, CustomAutoDisposeConverter<T> autoDisposeConverter, Consumer<T> consumer, Consumer<ApiException> apiExceptionConsumer) {
//        observable.as(autoDisposeConverter.getAutoDisposeConverter())
//                 .subscribe(new CommonSubscriber<T>(autoDisposeConverter.getiView(),autoDisposeConverter.isShowLoading(),autoDisposeConverter.isPageLoading(),apiExceptionConsumer) {
//                    @Override
//                    public void onNext(T t) {
//                        super.onNext(t);
//                        try {
//                            consumer.accept(t);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                });
//    }

    /**
     * 订阅连接
     * @param local
     * @param network
     * @param <T>
     * @return
     */
    public static <T> Observable<T> concat(Observable<T> local, Observable<T> network) {
        return Observable.concat(local, network).switchIfEmpty(ResponseTransformer.handleResultEmpty());
    }



}
